package com.xr.system;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.xr.common.Tools;

 
/**
 * employeeBizRight_dataRightDatasetListener
 */
public class employeeBizRight_dataRightDatasetListener extends AbstractDatasetListener {
	  public boolean beforeLoadData(Dataset dataset)
	     throws Exception {
	  	String superDeptId=Tools.toStr(dataset.parameters().getString("super_dept_id"));
	  	String employeeId=Tools.toStr(dataset.parameters().getString("employee_id"));
	  	//String providerId=Tools.toStr(dataset.parameters().getString("provider_id"));
        String belong_provider_id=Tools.toStr(dataset.parameters().getString("belong_provider_id"));
	  	
	  	String sql="";  	
	  	
	  	if (!superDeptId.equals("") &&  !superDeptId.equals("null"))  {
	  		sql="select a.isChecked,a.provider_id,a.provider_name " +
				"from " + 
				"(select distinct 1 isChecked,df.provider_id,df.provider_name from rt_pvd_emp_scope scp,rt_pvd_dept_employee de,pb_provider df " + 
				"where scp.fe_empid2=de.employee_id and de.provider_id=df.provider_id " +
				"and scp.fe_empid1="+employeeId+" and de.deleted=0 and df.IS_VALID=1 "+
				" and scp.fe_empid1<>scp.fe_empid2 and  df.type=1 and df.super_dept_id =" + superDeptId +
				" and df.belong_provider_id=" + belong_provider_id +
				" union " +
				"select 0 isChecked,df.provider_id,df.provider_name " + 
				"from pb_provider df " +
				"where df.IS_VALID=1  and df.super_dept_id=" + superDeptId +"  and df.provider_id not in (" + 
				"select distinct df1.provider_id from rt_pvd_emp_scope scp,rt_pvd_dept_employee de,pb_provider df1 " +
				"where scp.fe_empid2=de.employee_id and de.provider_id=df1.provider_id " +
				"and scp.fe_empid1="+employeeId+" and de.deleted=0 and df1.IS_VALID=1 "+
				" and scp.fe_empid1<>scp.fe_empid2 and df1.type=1 and df1.super_dept_id=" + superDeptId +
				" and df1.belong_provider_id=" + belong_provider_id +" ) and df.type=1 and df.belong_provider_id=" + belong_provider_id +
				" ) a  order by a.provider_id " ;
	  	}
	  	else  {
	  		sql="select a.isChecked,a.provider_id,a.provider_name " +
				"from " + 
				"(select distinct 1 isChecked,df.provider_id,df.provider_name from rt_pvd_emp_scope scp,rt_pvd_dept_employee de,pb_provider df " + 
				"where scp.fe_empid2=de.employee_id and de.provider_id=df.provider_id " +
				"and scp.fe_empid1="+employeeId+" and de.deleted=0 and df.IS_VALID=1 "+
				" and scp.fe_empid1<>scp.fe_empid2 and df.super_dept_id is null and df.type=1 and df.belong_provider_id=" + belong_provider_id +
				" union " +
				"select 0 isChecked,df.provider_id,df.provider_name " + 
				"from pb_provider df " +
				"where df.IS_VALID=1  and df.super_dept_id is null  and df.provider_id not in (" + 
				"select distinct df1.provider_id from rt_pvd_emp_scope scp,rt_pvd_dept_employee de,pb_provider df1 " +
				"where scp.fe_empid2=de.employee_id and de.provider_id=df1.provider_id " +
				"and scp.fe_empid1="+employeeId+" and de.deleted=0 and df1.IS_VALID=1 "+
				" and scp.fe_empid1<>scp.fe_empid2 and df1.type=1 and df1.super_dept_id is null and df1.belong_provider_id=" + belong_provider_id +
				") and df.type=1 and df.belong_provider_id=" + belong_provider_id +" ) a  order by a.provider_id " ;
	  	}
	  	System.out.println(sql);
		SqlDataset sqlDataset=(SqlDataset)dataset;
		sqlDataset.setSql(sql);
	    return true;
	  }

}
